﻿using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using eCommMongo.Core;
using eCommMongo.Core.Domain.Customers;
using eCommMongo.Core.Domain.Pictures;
using eCommMongo.Service.Pictures;
using eCommMongo.Service.Shops;

namespace eCommMongo.Service.Customers
{
    public class CustomerMapper
    {
        public static IList<Customer> ToCustomers(IList<Customer> customers)
        {
            return customers.Select(ToCustomer).ToList();
        }


        public static Customer ToCustomer(Customer customer)
        {
            var picService = EngineContext.Current.Resolve<IPictureService>();
            if (customer.Shop != null)
            {
                customer.Shop = ShopMapper.ToShop(customer.Shop.Id);
            }

           
            if (customer.CustomerLogo != null)
            {
                var picture = picService.GetPictureById(customer.CustomerLogo.Picture.Id);

                customer.CustomerLogo.Picture.PicUrl = picService.GetPictureUrl(
                    picture, 80, true);
            }
            else
            {


                customer.CustomerLogo = new CustomerLogo()
                {
                    Picture = new Picture()
                    {
                        PicUrl = picService.GetDefaultPictureUrl(GetCustomerLogoSize())
                    }
                };
            }

            return customer;
        }


        private static int GetCustomerLogoSize()
        {
            return int.Parse(ConfigurationManager.AppSettings["CustomerLogoSize"]);
        }

      
    }
}